package com.ltu.listenre;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import com.ltu.enums.EnumUtils;
import com.ltu.enums.RedisBusinessDict;
import com.ltu.model.request.RedisRollbackObj;
import com.ltu.model.request.RollBackKey;

import lombok.extern.slf4j.Slf4j;

/**  
 * @Description: springReids方式监听key过期
 * @author: 若尘  
 * @date 2019年7月7日 上午11:35:53
 * @version V1.0  
 */
@Slf4j
//@Component
public class RedisMessageListen implements MessageListener {

	@Autowired
	private RedisTemplate<String, String> redisTemplate;
	@Autowired
//	private DataMedicalDeviceService dataMedicalDeviceService;
	
	@Override
	public void onMessage(Message message, byte[] pattern) {
//		String channel = new String(message.getChannel());
		String str = (String) redisTemplate.getValueSerializer().deserialize(message.getBody());
		System.out.println("\n 失效的key:"+str);
		String[] rollBackKeyArr =str.split(":");
		if(rollBackKeyArr!=null && rollBackKeyArr[0].equals("RollBackKey")){
			RedisRollbackObj rollbackObj=	RollBackKey.getRollbackObj(rollBackKeyArr);
			StringBuilder sbStr=new StringBuilder();
			sbStr.append("\n \n api项目接收到过期的订单类型是： ");
			sbStr.append(rollbackObj.getTingType());
			sbStr.append(",出售类型是：");
			sbStr.append(rollbackObj.getSaleType());
			sbStr.append("，订单id:");
			sbStr.append(rollbackObj.getThingId());
			log.info(sbStr.toString());
			System.out.println("\n 过期的key："+sbStr.toString());
			RedisBusinessDict.BusinessName  dict=EnumUtils.indexToEnum(RedisBusinessDict.BusinessName.class, rollbackObj.getSaleType());
			switch(dict){
			case RecommandationRate:
				/* 评星处理 */
//				dataMedicalDeviceService.customerStarLevel( Integer.parseInt( rollbackObj.getThingId()) );
				break;
			
		
			}
			
		}
		
		
	}

}
